import './public-path.js';
// import 'default-passive-events';
import { createApp } from 'vue';
import App from './App.vue';
import router from './router/index';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import * as echarts from 'echarts'; // 引入echarts
import '@/assets/css/base.scss';
import './assets/iconfont/iconfont.css';
import './plugins/antvG6/index';
import store from './store/index';
import Highlight from './plugins/highlight';
import resize from './plugins/directive/resize';
// import vulTable from '@/components/vul-table';

let app: any = null;
function render(props: any) {
  const { container } = props;

  app = createApp(App);
  // 全局注册组件
  importAllComponents(app);
  // 图标注册
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
  }
  // 全局注册 echarts
  app.config.globalProperties.$echarts = echarts;
  app
    .use(router)
    .use(store)
    .use(Highlight)
    .use(resize)
    // .use(vulTable)
    .use(ElementPlus, { locale: zhCn })
    .mount(container ? container.querySelector('#app') : '#app');
}

// 独立运行时
if (!(window as any).__POWERED_BY_QIANKUN__) {
  render({});
}

/**
 * bootstrap ： 在微应用初始化的时候调用一次，之后的生命周期里不再调用
 */
export async function bootstrap() {
  // console.log('bootstrap')
}

/**
 * mount ： 在应用每次进入时调用
 */
export async function mount(props: any) {
  render(props);
}

/**
 * unmount ：应用每次 切出/卸载 均会调用
 */
export async function unmount() {
  app.unmount();
  app._container.innerHTML = '';
  app = null;
}
function importAllComponents(app) {
  // 使用 require.context 自动导入 components 文件夹下的所有 .vue 文件
  const componentsContext = require.context(
    './components/Attack',
    true,
    /\.vue$/,
  );
  componentsContext.keys().forEach((componentPath) => {
    const componentConfig = componentsContext(componentPath);
    const componentName = componentPath
      .split('/')
      .pop()
      ?.replace(/\.\w+$/, '');
    // 全局注册组件
    app.component(
      componentName ?? 'defaultComponentName',
      componentConfig.default || componentConfig,
    );
  });
}
